package com.atguigu.spzx.manager.service.impl;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.manager.service.OrderInfoService;
import com.atguigu.spzx.model.dto.system.OrderStatisticsDto;
import com.atguigu.spzx.model.entity.system.OrderStatistics;
import com.atguigu.spzx.model.vo.system.OrderStatisticsVo;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {
        // 查询统计结果数据
        List<OrderStatistics>  orderStatisticsList = orderStatisticsMapper.selectStatisticsList(orderStatisticsDto);
        //日期列表
        List<String> DateList = orderStatisticsList.stream()
                .map(orderStatistics -> DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd"))
                //Collectors.toList() 返回一个收集器（Collector），它可以将 Stream 流中的元素收集到一个新的列表中
                .collect(Collectors.toList());
        // 统计金额列表
        List<BigDecimal> amountList = orderStatisticsList.stream().map(OrderStatistics::getTotalAmount).collect(Collectors.toList());
        // 创建OrderStatisticsVo对象封装响应结果数据
        OrderStatisticsVo orderStatisticsVo  = new OrderStatisticsVo();
        orderStatisticsVo.setAmountList(amountList);
        orderStatisticsVo.setDateList(DateList);

        // 返回数据
        return orderStatisticsVo;
    }
}
